A structure is a variable that serves as a container of other variables. The basic concept is illustrated in the figure below, where a structure called "car" and its fields are shown.

We create a structure named car that contains the field owner:
car.owner = 'marc'
car =
struct with field:
owner: ['marc']
We have just created a structure called car that contains only 1 field, owner. The owner is marc. Now, you add the fields engine, mileage and manufacturer. The field car.engine is actually another structure containing the fields capacity, turbo and diesel. The following code completes the implementation of the structure car.
car.engine.turbo = false;
car.engine.diesel = false;
car.engine.capacity = 1.4;
car.owner = 'marc';
car.mileage = 140000;
car.manufacturer = 'mazda';
% Print out car and car.engine
car
car.engine
car =
struct with fields:
owner: ['marc']
manufacturer: ['mazda']
mileage: [1.4000]
engine: Struct [1 x 1]
ans =
struct with fields:
capacity: [1.4000]
diesel: [0]
turbo: [0]
In the above code, car is a structure (or structure scalar), which contains variables representing its attributes:
These attributes are called the fields of car. Fields of a structure may have different data types. For examples, owner is a character array and mileage is a double. A structure can contain another structure as its field. Here, car has the field engine, which is a structure containing other fields: capacity, turbo and diesel.
A structure array is an array containing structure scalars. In the above example, car is a structure array of 1 element. In the following example, marc_car, joe_car and ann_car are structures having the same fields. They can be catenated to form a structure array of 1 row: [marc_car, joe_car, ann_car].
marc_car.owner = 'marc';
marc_car.engine.turbo = false;
marc_car.engine.capacity = 1.4;
marc_car.engine.diesel = false;
marc_car.mileage = 140000;
marc_car.manufacturer = 'mazda';
joe_car.owner = 'joe';
joe_car.engine.turbo = false;
joe_car.engine.capacity = 1.0;
joe_car.engine.diesel = false;
joe_car.mileage = 40000;
joe_car.manufacturer = 'toyota';
ann_car.owner = 'an';
ann_car.engine.turbo = true;
ann_car.engine.capacity = 2.2;
ann_car.engine.diesel = false;
ann_car.mileage = 3000;
ann_car.manufacturer = 'subaru';
% Catenate structures to form a row.
cars = [marc_car, joe_car, ann_car]
cars =
struct array (1 x 3) with fields:
mileage, manufacturer, engine, owner
structThe second method is to use the built-in function struct. The following shows how the structures engine and car can be implemented. A field name is given as a character vector, followed by the field's value. The values can be any data type including double, logical, character, a structure, or function handle.
engine = struct('capacity', 1.4, 'turbo', false, 'diesel', false)
car = struct('engine', engine, 'owner', 'marc', 'manufacturer', 'mazda', 'mileage', 10456)
engine =
struct with fields:
capacity: [1.4000]
turbo: [0]
diesel: [0]
car =
struct with fields:
engine: Struct [1 x 1]
mileage: [10456.]
manufacturer: ['mazda']
owner: ['marc']
If no input argument is given, struct creates a structure with no fields:
struct
ans =
struct with no field
Structures contained in the same array must satisfy the following requirements:
Nevertheless, fields with the same names can contain data of different types and/or sizes. For example, we can add an electric car to the structure array cars. But, the engine fields (capacity, turbo and diesel) are not applicable to an electric motor. Hence, we simply assign the character array electric to the car's engine field:
john_car.mileage = 4000;
john_car.engine = 'electric';
john_car.manufacturer = 'tesla';
john_car.owner = 'john';
cars(4) = john_car
cars(4)
cars =
struct array (1 x 4) with fields:
engine, owner, mileage, manufacturer
ans =
struct with fields:
owner: ['john']
manufacturer: ['tesla']
mileage: [4000.0]
engine: ['electric']
We can add more fields to a structure array. If we want to specify that a Tesla car is cool, we add the field cool to cars(4):
cars(4).cool = true
cars =
struct array (1 x 4) with fields:
engine, owner, mileage, manufacturer, cool
We did not specify whether the other cars are cool or not. Hence, the field cool is an empty array by default for the other cars:
cars(1).cool
cars(2).cool
cars(3).cool
ans =
[] (double array)
ans =
[] (double array)
ans =
[] (double array)